import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:full_display/common/qxp_color.dart';
import 'package:full_display/common/qxp_style.dart';
import 'package:full_display/page/login/area_widget/area_code_widget.dart';
import 'package:full_display/routers/route_name.dart';
import 'package:full_display/unit/asset_utils/asset_utils.dart';
import 'package:full_display/unit/navigation/navigation_util.dart';
import 'package:full_display/unit/screen/screen_utility.dart';

class PhoneLogin extends StatefulWidget {
  final Function forgetPassword;
  final Function loginCallBack;

  ///type=0密码登录
  ///type=1找回密码
  int type;

  PhoneLogin({Key? key, required this.loginCallBack, required this.forgetPassword, this.type = 0}) : super(key: key);

  @override
  State<PhoneLogin> createState() => _PhoneLoginState();
}

class _PhoneLoginState extends State<PhoneLogin> {
  final TextEditingController _phoneController = TextEditingController();
  final TextEditingController _controller = TextEditingController();
  var _areaCode = '+86';

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            GestureDetector(
              onTap: () {
                NavigationUtil.getInstance()
                    .pushPage(context, RouteNames.areaCodeWidget, widget: const AreaCodeWidget(), func: (value) {
                  setState(() {
                    var valueStr = value['key'];
                    _areaCode = valueStr;
                  });
                });
              },
              child: Row(
                children: [
                  Text(
                    _areaCode,
                    style: StandardTextStyle.inputHintStyle,
                  ),
                  Container(
                    margin: EdgeInsets.only(left: 4.0.W),
                    width: 14.0.W,
                    height: 8.0.H,
                    child: Image.asset(AssetUtils.getAssetImage('icon_down_arrow')),
                  )
                ],
              ),
            ),
            Expanded(
                child: Container(
              margin: EdgeInsets.only(left: 10.0.W),
              child: TextField(
                onChanged: (text) {
                  _handleInput('phone', text);
                },
                maxLines: 1,
                maxLength: 11,
                controller: _phoneController,
                style: StandardTextStyle.inputHintStyle,
                decoration: InputDecoration(
                    counterText: '',
                    hintText: '请输入手机号',
                    hintStyle: StandardTextStyle.inputHintStyle,
                    border: InputBorder.none),
              ),
            ))
          ],
        ),
        Divider(
          height: 0.5.H,
          color: ColorPlate.white.withOpacity(0.15),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Expanded(
                child: Container(
              height: 67.0.H,
              margin: EdgeInsets.only(left: 71.0.W),
              child: TextField(
                onChanged: (text) {
                  _handleInput('password', text);
                },
                controller: _controller,
                style: StandardTextStyle.inputHintStyle,
                inputFormatters: [FilteringTextInputFormatter.allow(RegExp(r'[a-zA-Z0-9\W]'))],
                decoration: InputDecoration(
                    hintText: widget.type == 0 ? '请输入密码' : '请输入验证码',
                    hintStyle: StandardTextStyle.inputHintStyle,
                    border: InputBorder.none),
              ),
            )),
            GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () {
                //密码登录
                if (widget.type == 0) {
                  widget.loginCallBack.call(map);
                } else {
                  widget.forgetPassword.call();
                }
              },
              child: Text(
                widget.type == 0 ? "忘记密码" : "获取验证码",
                style: StandardTextStyle.textBlue16Style,
              ),
            )
          ],
        ),
        Divider(
          height: 0.5.H,
          color: ColorPlate.white.withOpacity(0.15),
        ),
      ],
    );
  }

  var map = {'phone': '', 'password': ''};

  void _handleInput(String type, String text) {
    if (type == 'phone') {
      map['phone'] = text;
    } else {
      map['password'] = text;
    }
    widget.loginCallBack.call(map);
  }
}
